import request from "../../utils/request"
let isSent = false;
// pages/search/search.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
        placeholderContent: '',
        hotlist: [],
        searchContent: '',
        searchResultList: [],
        historyList: []
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.getInitData();
    },

    async getInitData() {
        let {
            data: placeholderData
        } = await request('/search/default');
        let {
            data: hotlistData
        } = await request('/search/hot/detail');

        this.setData({
            placeholderContent: placeholderData.data.showKeyword,
            hotlist: hotlistData.data
        })

        // console.log(placeholderData, hotlistData);

        // 获取初始历史记录数据
        let historyList = wx.getStorageSync('historyList');
        if (historyList) {
            this.setData({
                historyList,
                searchResultList: [],
            })
        }
    },



    handleInput(e) {

        this.setData({
            searchContent: e.detail.value.trim()
        })
        if (isSent) {
            return;
        }
        isSent = true;
        this.getSearchResult();

        setTimeout(() => {
            isSent = false;
        }, 1000)


    },

    clearSearchContent() {
        this.setData({
            searchContent: ''
        })
    },

    deleteSearchHistory() {
        wx.showModal({
            content: '确认删除吗？',
            cancelColor: 'cancelColor',
            success: (res) => {
                if (res.confirm) {
                    this.setData({
                        historyList: []
                    });
                    wx.removeStorageSync('historyList')
                }
            }
        })
    },

    async getSearchResult() {
        if (!this.data.searchContent) {
            return;
        }

        let {
            searchContent,
            historyList
        } = this.data;

        let {
            data: searchResult
        } = await request('/search', {
            keywords: searchContent,
            limit: 10
        });


        this.setData({
            searchResultList: searchResult.result.songs
        });


        if (historyList.indexOf(searchContent) !== -1) {
            historyList.splice(historyList.indexOf(searchContent), 1);
        }
        historyList.unshift(searchContent);
        this.setData({
            historyList
        });
        wx.setStorageSync('historyList', historyList);


    },


    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})